package com.lesshassles.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.lesshassles.model.Task;
import com.lesshassles.model.TaskBroker;

/**
 * Controller for the User List screen.
 * @author David Santoro 
 */
public class TaskViewController implements Controller
{
	private String successView;
	private TaskBroker taskBroker;
    public static final String TASK_MAP_KEY = "task";

	/**
     * Returns a list of User database objects in ModelAndView.
     * @see com.lesshassles.model.User
     */
    public ModelAndView handleRequest(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
    	String[] urlParameters = 
    		request.getRequestURI().replace(".htm", "").split("-");
    	
    	int taskId = Integer.parseInt(urlParameters[urlParameters.length-1]);
    	
    	Task task = taskBroker.getById(taskId);
    	
    	ModelAndView modelAndView = new ModelAndView(getSuccessView());
    	modelAndView.addObject(TASK_MAP_KEY, task);
    	
        return modelAndView;
    }

	public String getSuccessView() {
		return successView;
	}
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public TaskBroker getTaskBroker() {
		return taskBroker;
	}
	public void setTaskBroker(TaskBroker taskBroker) {
		this.taskBroker = taskBroker;
	}
}
